package com.spring.hibernate.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.spring.hibernate.dao.ContactsDAO;
import com.spring.hibernate.entity.Contact;
import com.spring.hibernate.validator.ContactFormValidator;

@Controller
public class ContactsController extends AbstractController {

    @Autowired
    private ContactsDAO          contactsDAO;
    @Autowired
    private ContactFormValidator contactValidator;



    @SuppressWarnings("rawtypes")
    @RequestMapping("/searchContacts")
    public ModelAndView searchContacts(@RequestParam(required = false, defaultValue = "")
    String name) {
        ModelAndView mav = new ModelAndView("showContacts");
        List contacts = contactsDAO.searchContacts(name.trim());
        mav.addObject("SEARCH_CONTACTS_RESULTS_KEY", contacts);
        return mav;
    }



    @SuppressWarnings("rawtypes")
    @RequestMapping("/viewAllContacts")
    public ModelAndView getAllContacts() {
        ModelAndView mav = new ModelAndView("showContacts");
        List contacts = contactsDAO.getAllContacts();
        mav.addObject("SEARCH_CONTACTS_RESULTS_KEY", contacts);
        return mav;
    }



    @RequestMapping(value = "/saveContact", method = RequestMethod.GET)
    public ModelAndView newUserForm() {
        ModelAndView mav = new ModelAndView("newContact");
        Contact contact = new Contact();
        mav.getModelMap().put("newContact", contact);
        return mav;
    }



    @RequestMapping(value = "/saveContact", method = RequestMethod.POST)
    public String create(@ModelAttribute("newContact")
    Contact contact, BindingResult result, SessionStatus status) {
        contactValidator.validate(contact, result);
        if (result.hasErrors()) {
            return "newContact";
        }
        contactsDAO.save(contact);
        status.setComplete();
        return "redirect:viewAllContacts.do";
    }



    @RequestMapping(value = "/updateContact", method = RequestMethod.GET)
    public ModelAndView edit(@RequestParam("id")
    Integer id) {
        ModelAndView mav = new ModelAndView("editContact");
        Contact contact = contactsDAO.getById(id);
        mav.addObject("editContact", contact);
        return mav;
    }



    @RequestMapping(value = "/updateContact", method = RequestMethod.POST)
    public String update(@ModelAttribute("editContact")
    Contact contact, BindingResult result, SessionStatus status) {
        contactValidator.validate(contact, result);
        if (result.hasErrors()) {
            return "editContact";
        }
        contactsDAO.update(contact);
        status.setComplete();
        return "redirect:viewAllContacts.do";
    }



    @RequestMapping("deleteContact")
    public ModelAndView delete(@RequestParam("id")
    Integer id) {
        ModelAndView mav = new ModelAndView("redirect:viewAllContacts.do");
        contactsDAO.delete(id);
        return mav;
    }
}
